<template>
  <div>
    <el-upload
      action="/resource/goods/addImgs"
      list-type="picture-card"
      :on-success="handleAvatarSuccess"
  
      :auto-upload="true"
      :file-list="fileList"
    >
      <i slot="default" class="el-icon-plus"></i>

      <div slot="file" slot-scope="{ file }">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleDownload(file)"
          >
            <i class="el-icon-download"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <el-button @click="updataImgGoods" type="primary"
      >上传<i class="el-icon-upload el-icon--right"></i
    ></el-button>
  </div>
</template>
<script>
export default {
  mounted() {},
  props: {
    imagesRow: {
      type: Object,
      default: () => {},
    },
    isUpdataImg:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      imageUrl: "",
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      fileList: [{ name: "", url: this.imagesRow.images }],
    };
  },
  methods: {
    updataImgGoods() {
      const _id = this.imagesRow._id;
      if(this.$data.imageUrl===""){
        this.$emit('isDataImg');
        alert("上传图片不能为空")
      }
      this.$emit('updataImg',{_id,images:this.$data.imageUrl});

    },

    handleAvatarSuccess(res){
       this.$data.imageUrl = `http://localhost:3000${res.data.url}`;
     
    },

    handleRemove() {
      this.$data.fileList = [];
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
};
</script>